package SocketMake;

import SeverMake.SeverMaking;
import Tools.GetScanner;
import Tools.User;
import Tools.UserMessage;

import java.io.*;
import java.net.Socket;

public class SocketMaking {
    private ObjectOutputStream oos = null;
    private ObjectInputStream ois = null;
    private Socket socket;
    static File testFile = new File("d:/test.txt");
    public void socketStart() throws IOException, InterruptedException, ClassNotFoundException {
        socket = new Socket("192.168.1.3",8888);
        System.out.println("连接服务器成功！");
        oos = new ObjectOutputStream(socket.getOutputStream());
        ois = new ObjectInputStream(socket.getInputStream());
        socketView();
    }

    public void socketClose() throws IOException {
        if (null != socket){
            socket.close();
        }
        GetScanner.closeSc();
    }

    public void socketView() throws InterruptedException, IOException, ClassNotFoundException {
        while (true){
            System.out.println("-------------------------------------");
            System.out.println("欢迎进入管理界面,请输入选择");
            System.out.println("   [1] 学员管理系统  [2] 管理员系统");
            System.out.println("   [0] 退出系统！");
            System.out.println("-------------------------------------");
            System.out.println("请输入选择！");
            int choose = GetScanner.getSc().nextInt();
            switch (choose){
                case 1:
                    System.out.println("进入学员管理界面");
                    studenLogin();
                    break;
                case 2:
                    System.out.println("进入管理员界面");
                    managerLogin();
                    break;
                case 0:
                    System.out.println("退出系统！");
                    closeSever();
                    return;
            }
        }
    }

    private void studenLogin() throws IOException, ClassNotFoundException {
        System.out.println("请输入学员账户！");
        String name = GetScanner.getSc().next();
        System.out.println("请输入学员账户密码！");
        String password = GetScanner.getSc().next();
        oos.writeObject(new UserMessage("studentCheck",new User(name,password)));
        System.out.println("客户端发送数据成功！");
        UserMessage um = (UserMessage) ois.readObject();
        if ("success".equalsIgnoreCase(um.getType())){
            System.out.println("登录成功！");
            testView();
        }else {
            System.out.println("登录失败！");
        }
    }

    private void testView() {
        while (true){
            System.out.println("-------考试模块--------");
            System.out.println("   [1] 准备考试 [2] 导出成绩");
            System.out.println("   [3] 查询成绩 [0] 退出系统");
            System.out.println("-------------------------");
            System.out.println("请输入你的选择！");
            int choose = GetScanner.getSc().nextInt();
            switch (choose){
                case 1:
                    System.out.println("考试开始！");
                    break;
                case 2:
                    System.out.println("导出成绩！");
                    break;
                case 3:
                    System.out.println("查询成绩！");
                    break;
                case 0:
                    return;
            }
        }
    }


    public void closeSever() throws IOException {
        oos.writeObject(new UserMessage("exit",new User("aaa","aaa")));
    }

    private void managerLogin() throws IOException, ClassNotFoundException {

        System.out.println("请输入管理员账户！");
        String name = GetScanner.getSc().next();
        System.out.println("请输入管理员账户密码！");
        String password = GetScanner.getSc().next();
        oos.writeObject(new UserMessage("managerCheck",new User(name,password)));
        System.out.println("客户端发送数据成功！");
        UserMessage um = (UserMessage) ois.readObject();
        if ("success".equalsIgnoreCase(um.getType())){
            System.out.println("登录成功！");
            studentManage();
        }else {
            System.out.println("登录失败！");
        }
    }

    private void studentManage() throws IOException, ClassNotFoundException {
        while (true){
            System.out.println("---------------------");
            System.out.println("欢迎进入学员管理界面！");
            System.out.println("   [1] 添加学员  [2] 修改学员信息  [3] 考题管理");
            System.out.println("   [0] 退出学员管理界面");
            System.out.println("---------------------");
            System.out.println("请输入选择！");
            int choosen = GetScanner.getSc().nextInt();
            switch (choosen){
                case 1:
                    System.out.println("正在添加学员！");
                    addStudent();
                    break;
                case 2:
                    System.out.println("正在修改学员信息！");
                    changeStudent();
                    break;
                case 3:
                    System.out.println("进入考题管理界面！");
                    manageTest();
                    break;
                case 0:
                    System.out.println("正在退出学员管理系统");
                    closeSever();
                    return;
            }
        }
    }

    private void manageTest() throws IOException {
        while (true){
            System.out.println("-------欢迎进入考题管理---------");
            System.out.println("   [1] 增加考题 [2] 删除考题    ");
            System.out.println("   [3] 查询考题 [0] 退出考题管理  ");
            System.out.println("------------------------------");
            System.out.println("请输入你的选择！");
            int i = GetScanner.getSc().nextInt();
            switch (i) {
                case 1:
                    System.out.println("增加考题");
                    addTestTip();
                    break;
                case 2:
                    System.out.println("删除考题");
                    deleteTestTip();
                    break;
                case 3:
                    System.out.println("查询考题");
                    lookUpTest();
                    break;
                case 0:
                    System.out.println("退出考题管理！");
                    return;
            }
        }
    }

    private void deleteTestTip() throws IOException {
        boolean delete = testFile.delete();
        System.out.println("删除成功！？");

    }

    private void lookUpTest() throws IOException {
        if (testFile.exists()){
            BufferedReader br = new BufferedReader(new FileReader(testFile));
            String s = null;
            while ( (s = br.readLine()) != null){
                System.out.println(s);
            }
            br.close();
        }else {
            testFile.createNewFile();
        }
    }

    private void addTestTip() throws IOException {
        FileWriter fw = new FileWriter(testFile,true);
        System.out.println("请输入要添加的考题！");
        String testTip = GetScanner.getSc().next();
        fw.write(testTip);
        fw.close();
    }

    private void changeStudent() throws IOException, ClassNotFoundException {
        System.out.println("请输入要修改的学员姓名");
        String name = GetScanner.getSc().next();
        System.out.println("请输入要修改学员的密码！");
        String password = GetScanner.getSc().next();
        oos.writeObject(new UserMessage("changeStudent",new User(name,password)));
        System.out.println("要修改的学员的信息发送成功！");
        UserMessage um = (UserMessage) ois.readObject();
        if ("success".equalsIgnoreCase(um.getType())){
            System.out.println("修改成功！！");
        }else {
            System.out.println("无法修改该学员的信息！！");
        }
    }

    private void addStudent() throws IOException, ClassNotFoundException {
        System.out.println("请输入要添加学生的姓名");
        String name = GetScanner.getSc().next();
        System.out.println("请输入要添加学生的密码！");
        String password = GetScanner.getSc().next();
        oos.writeObject(new UserMessage("addStudent",new User(name,password)));
        System.out.println("要添加学生的信息发送成功！");
        UserMessage um = (UserMessage) ois.readObject();
        System.out.println("接收服务器发送的内容成功！");
        if ("success".equalsIgnoreCase(um.getType())){
            System.out.println("添加学员成功！");
        }else {
            System.out.println("该学生已存在无法添加！");
        }
    }


}
